package com.neothefox.romashka;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.inputmethodservice.InputMethodService;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;

public class RomashkaIME extends InputMethodService implements
OnSharedPreferenceChangeListener{
	
	SharedPreferences prefs;
	RomashkaView inputView;
	int jx, jy, cx, mod, lay;
	private Layout layout;
	InputMethodManager manager;
		
	@Override
	public void onStartInputView(EditorInfo info, boolean restarting) {
		super.onStartInputView(info, restarting);
	}

	public RomashkaIME() {
		
	}
	
	 @Override 
	 public View onCreateInputView() {
		 
		 prefs = getApplicationContext().getSharedPreferences("ROMASHKA_IME", MODE_PRIVATE);
		 prefs.registerOnSharedPreferenceChangeListener(this);
		 getApplicationContext();
		 manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		 //parseLayout(new File)
		 layout = new Layout();
		 layout.setMain();
		 inputView = 
				 (RomashkaView)getLayoutInflater().inflate(R.layout.input, null);
		 inputView.setAlpha(prefs.getInt("trans", 0));
		 inputView.setOffset(
				 prefs.getInt("xOff", 200),
				 prefs.getInt("yOff", 200)
				 );
		 jx = 0; jy = 0; cx = 0; mod = 1; lay = 0;
		 return inputView; 
	 }
	 
	 @Override
	 public void onCreate() {
		 super.onCreate();
		 
	 }
	 
	 @Override
	 public void onInitializeInterface() {
		 super.onInitializeInterface();
		 
	 }
	 
	 @Override
		public boolean onKeyDown(int keyCode, KeyEvent event)
		{
			if(!this.isInputViewShown())
			{
				super.onKeyDown(keyCode, event);
			}
			switch(keyCode)
			{
			case KeyEvent.KEYCODE_DPAD_UP:
				jx = -1;
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				jx = 1;
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				jy = -1;
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				jy = 1;
				break;
			case KeyEvent.KEYCODE_BUTTON_X:
				cx = 3;
				break;
			case KeyEvent.KEYCODE_BUTTON_B:
				cx = 1;
				break;
			case KeyEvent.KEYCODE_BUTTON_Y:
				cx = 2;
				break;
			case KeyEvent.KEYCODE_BUTTON_A:
				cx = 0;
				break;
			case KeyEvent.KEYCODE_BUTTON_L1:
				mod = 0;
				break;
			default:
				//super.onKeyDown(keyCode, event);
				return false;
			}
			inputView.setCircleSector(jx,jy);
			inputView.setTextSector(cx);
			inputView.setMod(mod);
			inputView.invalidate();
			return true;
		}
		
		@Override
		public boolean onKeyUp(int keyCode, KeyEvent event)
		{
		if(!this.isInputViewShown())
		{
			super.onKeyUp(keyCode, event);
		}
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_UP:
		case KeyEvent.KEYCODE_DPAD_DOWN:
			jx = 0;
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			jy = 0;
			break;
		case KeyEvent.KEYCODE_BUTTON_X:
		case KeyEvent.KEYCODE_BUTTON_B:
		case KeyEvent.KEYCODE_BUTTON_Y:
		case KeyEvent.KEYCODE_BUTTON_A:
			if(inputView.getCircleSector() == -1)
			{
				switch(cx)
				{
				case 2: //y
					//getCurrentInputConnection().deleteSurroundingText(1, 1);
					sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
					break;
				case 0: //a
					//getCurrentInputConnection().commitText(" ", 1);
					sendDownUpKeyEvents(KeyEvent.KEYCODE_SPACE);
					break;
				case 1: //b
					sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
					break;
				case 3: //x
					sendDownUpKeyEvents(KeyEvent.KEYCODE_BACK);
					//this.manager.hideSoftInputFromInputMethod(getCurrentInputBinding().getConnectionToken(), flags));
					break;
				}
				break;
			}
			getCurrentInputConnection().commitText(
					layout.charmap[inputView.getCircleSector()][mod][cx], 1
					);
			break;
		case KeyEvent.KEYCODE_BUTTON_L1:
			mod = 1;
			break;
		case KeyEvent.KEYCODE_BUTTON_R1:
			if(layout.type == Layout.TYPE.MAIN|| layout.type == Layout.TYPE.USER)
			{
				layout.setNumeric();
				inputView.setLayout(Layout.TYPE.NUMERIC);
			}
			else if(layout.type == Layout.TYPE.NUMERIC || layout.type == Layout.TYPE.USER)
			{
				layout.setMain();
				inputView.setLayout(Layout.TYPE.MAIN);
			}
			break;
			
		case KeyEvent.KEYCODE_BUTTON_R2:
			if((layout.type == Layout.TYPE.MAIN || layout.type == Layout.TYPE.NUMERIC) && prefs.getBoolean("userLayout", false))
			{
				try {
					layout.setUser(new File(prefs.getString("layoutPath",getString(R.string.default_path))));
					inputView.setUserLayout(prefs.getString("layoutPath",getString(R.string.default_path)));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			else if(layout.type == Layout.TYPE.USER)
			{
				layout.setMain();
				inputView.setLayout(Layout.TYPE.MAIN);
			}
			break;
			
		default:
			//super.onKeyDown(keyCode, event);
			return false;
		}
		cx = 0;
		inputView.setCircleSector(jx,jy);
		inputView.setTextSector(-1);
		inputView.setMod(mod);
		inputView.invalidate();
		return true;
		}

		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
				inputView.setAlpha(prefs.getInt("trans", 0));
				inputView.setOffset(
					 prefs.getInt("xOff", 200),
					 prefs.getInt("yOff", 200)
					 );
		}
		
		public void sendDownUpKeyEvents(int keyEventCode) {
	        InputConnection ic = getCurrentInputConnection();
	        if (ic == null) return;
	        long eventTime = SystemClock.uptimeMillis();
	        ic.sendKeyEvent(new KeyEvent(eventTime, eventTime,
	                KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0,
	                KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
	        ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
	                KeyEvent.ACTION_UP, keyEventCode, 0, 0, 0, 0,
	                KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
	    }
	 
}

